import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

let beforeEnter = (to, from, next) => {
  let userId = localStorage.getItem("userId")
  if (!userId) {
    next('/Login');
  } else {
    next()
  }
}

const routes = [
  // 根目录重定向
  {
    path: '/',
    redirect: '/index'
  },
  // 主页
  {
    path: '/index',
    name: 'index',
    component: () => import('../views/indexView.vue')
  },
  // 搜索页
  {
    path: '/search',
    name: 'search',
    component: () => import('../views/searchView.vue')
  },
  // 书架页
  {
    path: '/bookshelf',
    name: 'bookshelf',
    component: () => import('../views/bookShelf.vue'),
    beforeEnter
  },
  // 个人页面
  {
    path: '/My',
    name: 'My',
    component: () => import('../views/MyView.vue'),
    beforeEnter,
  },
  // 注册页面
  {
    path: '/register',
    name: 'register',
    component: () => import('../views/registerView.vue')
  },
  // 登录页面
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/loginView.vue')
  },
  // 详情页
  {
    path: '/details',
    name: 'details',
    component: () => import('../views/detailsView.vue'),
    beforeEnter
  },
  // 内容页
  {
    path: '/content',
    name: 'content',
    component: () => import('../views/contentView.vue')
  },
  // 分类页
  {
    path: '/classify',
    name: 'classify',
    component: () => import('../views/classifyView.vue')
  },
  // vip页面
  {
    path: '/vip',
    name: 'vip',
    component: () => import('../views/vipView.vue')
  },
  // 充值页面
  {
    path: '/recharge',
    name: 'recharge',
    component: () => import('../views/rechargeView.vue')
  },
  // 充值页面
  {
    path: '/catalog',
    name: 'catalog',
    component: () => import('../views/catalogView.vue')
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router
